﻿using MyWPF.Models;
using MyWPF.Models.UserManagement;
using MyWPF.StaticData;
using MyWPF.Views.Others;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;

namespace MyWPF.ViewModel.User
{
    public class VM_UserWindow_AddUser : BaseViewModel
    {
        public VM_UserWindow_AddUser()
        {
            _close = new DelegateCommand<object>(_Close);
            _min = new DelegateCommand<object>(_Min);
            _moveWindow = new DelegateCommand<object>(_MoveWindow);
            _addUser = new DelegateCommand<object>(_AddUser);

            ItemsList = new List<string>
            {
                "游客",
                "操作员",
                "工程师",
                "管理员"
            };
        }
        #region 基础：前端事件
        /// <summary>
        /// 关闭窗口
        /// </summary>
        private ICommand _close;
        public ICommand Close
        {
            get { return _close; }
            set
            {
                _close = value;
                OnPropertyChanged("Close");
            }
        }
        private void _Close(object obj)
        {
            StaticModels.userWindow_AddUser.Close();
        }
        /// <summary>
        /// 最小化窗口
        /// </summary>
        private ICommand _min;
        public ICommand Min
        {
            get { return _min; }
            set
            {
                _min = value;
                OnPropertyChanged("Min");
            }
        }
        private void _Min(object obj)
        {
            StaticModels.userWindow_AddUser.WindowState = System.Windows.WindowState.Minimized;
        }
        /// <summary>
        /// 移动窗口
        /// </summary>
        private ICommand _moveWindow;
        public ICommand MoveWindow
        {
            get { return _moveWindow; }
            set
            {
                _moveWindow = value;
                OnPropertyChanged("MoveWindow");
            }
        }
        private void _MoveWindow(object obj)
        {
            StaticModels.userWindow_AddUser.DragMove();
        }

        private void OnPropertyChanged(string v)
        {
            throw new NotImplementedException();
        }
        #endregion


        #region 属性绑定：用户输入的参数
        /// <summary>
        /// 用户输入的用户名
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set { SetProperty(ref _userName, value); }
        }
        private string _userName;
        /// <summary>
        /// 用户输入的密码
        /// </summary>
        public string PassWord1
        {
            get { return _passWord1; }
            set { SetProperty(ref _passWord1, value); }
        }
        private string _passWord1;
        /// <summary>
        /// 用户输入的确认密码
        /// </summary>
        public string PassWord2
        {
            get { return _passWord2; }
            set { SetProperty(ref _passWord2, value); }
        }
        private string _passWord2;
        /// <summary>
        /// 权限列表
        /// </summary>
        public List<string> ItemsList
        {
            get { return _itemsList; }
            set { SetProperty(ref _itemsList, value); }
        }
        private List<string> _itemsList;
        /// <summary>
        /// 用户选择的权限，游客=0，操作员=1，工程师=2，管理员=3
        /// </summary>
        public string UserSelected
        {
            get { return _userSelected; }
            set 
            { 
                SetProperty(ref _userSelected, value); 
                switch (value)
                {
                    case "游客":
                        UserPower = 1000;
                        break;
                    case "操作员":
                        UserPower = 2000;
                        break;
                    case "工程师":
                        UserPower = 3000;
                        break;
                    case "管理员":
                        UserPower = 4000;
                        break;
                }
            }
        }
        private string _userSelected;
        private int UserPower;
        /// <summary>
        /// 用户输入的备注
        /// </summary>
        public string Remarks
        {
            get { return _remarks; }
            set { SetProperty(ref _remarks, value); }
        }
        private string _remarks;
        #endregion

        #region 事件绑定：添加用户
        /// <summary>
        /// 添加用户
        /// </summary>
        private ICommand _addUser;
        public ICommand AddUser
        {
            get { return _addUser; }
            set
            {
                _addUser = value;
                OnPropertyChanged("AddUser");
            }
        }
        private async void _AddUser(object obj)
        {
            UserContext User = new UserContext()
            {
                UserName = UserName,
                Password = PassWord1,
                PowerLevel = UserPower,
                Remarks = Remarks
            };
            switch(User.PowerLevel)
            {
                case 0:
                    User.Power = Models.UserManagement.UserPower.None;
                    break;
                case 1000:
                    User.Power = Models.UserManagement.UserPower.Tourist;
                    break;
                case 2000:
                    User.Power = Models.UserManagement.UserPower.Operator;
                    break;
                case 3000:
                    User.Power = Models.UserManagement.UserPower.Engineer;
                    break;
                case 4000:
                    User.Power = Models.UserManagement.UserPower.Administrator;
                    break;

            }

            #region 检测用户输入是否有误
            string error = "";
            if (UserName == null || UserName == "")
            {
                error = "用户名不能为空！\n";
            }
            if (StaticModels.user.GetUser_UserName(User).Rows.Count > 0)
            {
                error += "当前用户已存在！\n";
            }
            if (PassWord1== null || PassWord1 == "" || PassWord2 == null || PassWord2 == "")
            {
                error = error + "密码不能为空！\n";
            }
            if (PassWord1 != PassWord2)
            {
                error += "两次密码不一致！\n";
            }
            if (UserPower > User.PowerLevel)
            {
                error += "所选用户权限高于当前登录用户权限！\n";
            }
            if (error.Count() > 0)
            {
                error = "输入有误，存在以下问题：\n" + error;
                Tips tips = new Tips(error);
                tips.ShowDialog();
                return;
            }
            #endregion
            #region 信息满足要求，新增用户
            StaticModels.user.AddUser(User);

            //刷新用户列表
            StaticModels.vM_UserWindow.UserDataTable = StaticModels.user.GetUser_Level(StaticModels.user.User);

            //添加日志
            StaticModels.journal.AddJournal(new Models.Journal.JournalContext()
            {
                Context = "添加用户'" + User.UserName + "'",
                User = StaticModels.user.User.UserName
            });
            //提示用户
            Tips tips1 = new Tips("添加用户成功！");
            StaticModels.userWindow_AddUser.Close();
            tips1.ShowDialog();
            #endregion


        }
        #endregion
    }
}
